import i18n from '@app-base/common/utils/i18next';
import { ENTITY_TYPE_EDGE } from '@app-nextmesh/types';
import { RJSFSchema, UiSchema } from '@rjsf/utils';
import { SelectedEntitiesSchema } from '../jsonSchema';

export const pickTypes = [ENTITY_TYPE_EDGE];
export const schema: RJSFSchema = {
  type: 'object',
  properties: {
    Method: {
      type: 'string',
      title: i18n.t('Method'),
      oneOf: [
        {
          const: 'BySize',
          title: i18n.t('BySize'),
        },
        {
          const: 'ByNumber',
          title: i18n.t('ByNumber'),
        },
      ],
      default: 'BySize',
    },
    Bias: {
      type: 'string',
      title: i18n.t('Bias'),
      oneOf: [
        {
          const: 'None',
          title: i18n.t('None'),
        },
        {
          const: 'Linear',
          title: i18n.t('Linear'),
        },
        {
          const: 'Exponential',
          title: i18n.t('Exponential'),
        },
        {
          const: 'BellCurve',
          title: i18n.t('BellCurve'),
        },
      ],
      default: 'None',
    },
    ...SelectedEntitiesSchema,
  },
  allOf: [
    // Method==BySize && Bias==None/BellCurve，显示Size
    {
      if: {
        allOf: [
          {
            properties: {
              Method: {
                const: 'BySize',
              },
            },
          },
        ],
        anyOf: [
          {
            properties: {
              Bias: {
                const: 'None',
              },
            },
          },
          {
            properties: {
              Bias: {
                const: 'BellCurve',
              },
            },
          },
        ],
      },
      then: {
        properties: {
          Size: {
            type: 'number',
            title: i18n.t('Size'),
            default: 10,
            exclusiveMinimum: 0,
          },
        },
        required: ['Size'],
      },
    },
    // Method==BySize && Bias==Linear/Exponential，显示MaxSize和MinSize
    {
      if: {
        allOf: [
          {
            properties: {
              Method: {
                const: 'BySize',
              },
            },
          },
        ],
        anyOf: [
          {
            properties: {
              Bias: {
                const: 'Linear',
              },
            },
          },
          {
            properties: {
              Bias: {
                const: 'Exponential',
              },
            },
          },
        ],
      },
      then: {
        properties: {
          MaxSize: {
            type: 'number',
            title: i18n.t('MaxSize'),
            default: 10,
            exclusiveMinimum: 0,
          },
          MinSize: {
            type: 'number',
            title: i18n.t('MinSize'),
            default: 1,

            exclusiveMinimum: 0,
          },
        },
        required: ['MaxSize', 'MinSize'],
      },
    },
    // Method==BySize && Bias==BellCurve，显示BiasValue
    {
      if: {
        allOf: [
          {
            properties: {
              Method: {
                const: 'BySize',
              },
            },
          },
          {
            properties: {
              Bias: {
                const: 'BellCurve',
              },
            },
          },
        ],
      },
      then: {
        properties: {
          BiasValue: {
            type: 'number',
            title: i18n.t('BiasValue'),
            default: 1,
            minimum: 0,
          },
        },
        required: ['BiasValue'],
      },
    },
    // Method==ByNumber，显示Number
    {
      if: {
        allOf: [
          {
            properties: {
              Method: {
                const: 'ByNumber',
              },
            },
          },
        ],
      },
      then: {
        properties: {
          Number: {
            type: 'integer',
            title: i18n.t('Number'),
            default: 10,
            minimum: 1,
            maximum: 1e7,
          },
        },
        required: ['Number'],
      },
    },
    // Method==ByNumber && Bias==Linear/Exponential，显示BiasValue
    {
      if: {
        allOf: [
          {
            properties: {
              Method: {
                const: 'ByNumber',
              },
            },
          },
        ],
        oneOf: [
          {
            properties: {
              Bias: {
                const: 'Linear',
              },
            },
          },
          {
            properties: {
              Bias: {
                const: 'Exponential',
              },
            },
          },
          {
            properties: {
              Bias: {
                const: 'BellCurve',
              },
            },
          },
        ],
      },
      then: {
        properties: {
          BiasValue: {
            type: 'number',
            title: i18n.t('BiasValue'),
            default: 1,
            minimum: 0,
          },
        },
        required: ['BiasValue'],
      },
    },
    // Bias==None，不显示Reverse
    {
      if: {
        anyOf: [
          {
            not: {
              properties: {
                Bias: {
                  const: 'None',
                },
              },
            },
          },
        ],
      },
      then: {
        properties: {
          Reverse: {
            type: 'boolean',
            title: i18n.t('Reverse'),
            default: false,
          },
        },
        required: ['Reverse'],
      },
    },
  ],
};
export const uiSchema: UiSchema = {
  Method: {
    'ui:widget': 'radio',
  },
  Bias: {
    'ui:widget': 'radio',
  },
  SelectedEntities: {
    'ui:field': 'entity',
    pickTypes: pickTypes,
  },
  'ui:order': [
    'Method',
    'Bias',
    'Size',
    'MaxSize',
    'MinSize',
    'Number',
    'BiasValue',
    'Reverse',
    'SelectedEntities',
  ],
};
